<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"内容="width=device-width, initial-scale=1.0">
    <title>精美侧边栏</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f5f7fa;
            display: flex;
            min-height: 100vh;
        }

        /* 侧边栏容器 */
        .sidebar {
            width: 250px;
            background: linear-gradient(180deg, #2c3e50 0%, #1a2530 100%);
            color: white;
            height: 100vh;
            transition: all 0.4s cubic-bezier(0.68, -0.55, 0.265, 1.55);
            box-shadow: 3px 0 15px rgba(0, 0, 0, 0.1);
            position: relative;
            z-index: 100;
        }

        /* 折叠状态下的侧边栏 */
        .sidebar.collapsed {
            width: 70px;
        }

        /* 侧边栏头部 */
        .sidebar-header {
            padding: 25px 20px;
            display: flex;
            align-items: center;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

        .sidebar.collapsed .sidebar-header {
            justify-content: center;
            padding: 25px 0;
        }

        .logo {
            width: 40px;
            height: 40px;
            background: linear-gradient(135deg, #3498db, #8e44ad);
            border-radius: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            font-size: 20px;
            margin-right: 15px;
        }

        .sidebar.collapsed .logo {
            margin-right: 0;
        }

        .logo-text {
            font-size: 22px;
            font-weight: 600;
            transition: opacity 0.3s ease;
        }

        .sidebar.collapsed .logo-text {
            opacity: 0;
            width: 0;
            overflow: hidden;
        }

        /* 折叠按钮 */
        .toggle-btn {
            position: absolute;
            top: 25px;
            right: -12px;
            width: 24px;
            height: 24px;
            background: #3498db;
            border-radius: 50%;
            border: none;
            color: white;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
            transition: all 0.3s ease;
            z-index: 101;
        }

        .toggle-btn:hover {
            transform: scale(1.1);
            background: #2980b9;
        }

        .sidebar.collapsed .toggle-btn {
            right: -12px;
        }

        /* 导航菜单 */
        .nav-menu {
            padding: 20px 0;
        }

        .nav-item {
            display: flex;
            align-items: center;
            padding: 15px 25px;
            margin: 5px 15px;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }

        .nav-item:hover {
            background: rgba(255, 255, 255, 0.1);
            transform: translateX(5px);
        }

        .nav-item.active {
            background: rgba(52, 152, 219, 0.3);
        }

        .nav-icon {
            font-size: 20px;
            margin-right: 15px;
            width: 24px;
            text-align: center;
            transition: all 0.3s ease;
        }

        .sidebar.collapsed .nav-icon {
            margin-right: 0;
        }

        .nav-text {
            font-size: 16px;
            white-space: nowrap;
            transition: opacity 0.3s ease;
        }

        .sidebar.collapsed .nav-text {
            opacity: 0;
            width: 0;
            overflow: hidden;
        }

        /* Tooltip样式 */
        .tooltip {
            position: fixed;
            left: 70px;
            background: #2c3e50;
            color: white;
            padding: 10px 20px;
            border-radius: 6px;
            font-size: 14px;
            white-space: nowrap;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
            opacity: 0;
            visibility: hidden;
            transition: all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
            z-index: 1000;
            transform: translateX(-10px);
        }

        .tooltip::after {
            content: '';
            position: absolute;
            top: 50%;
            left: -6px;
            transform: translateY(-50%);
            border: 6px solid transparent;
            border-right-color: #2c3e50;
        }

        /* 主内容区域 */
        .main-content {
            flex: 1;
            padding: 30px;
            background: #f5f7fa;
        }

        .content-header {
            margin-bottom: 30px;
        }

        .content-header h1 {
            color: #2c3e50;
            margin-bottom: 10px;
        }

        .content-header p {
            color: #7f8c8d;
            font-size: 16px;
        }

        .card-container {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 25px;
        }

        .card {
            background: white;
            border-radius: 12px;
            padding: 25px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }

        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
        }

        .card h3 {
            color: #2c3e50;
            margin-bottom: 15px;
        }

        .card p {
            color: #7f8c8d;
            line-height: 1.6;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .sidebar {
                width: 70px;
            }
            
            .sidebar:not(.collapsed) {
                width: 250px;
            }
            
            .logo-text,
            .nav-text {
                opacity: 0;
                width: 0;
                overflow: hidden;
            }
            
            .sidebar:not(.collapsed) .logo-text,
            .sidebar:not(.collapsed) .nav-text {
                opacity: 1;
                width: auto;
            }
            
            .sidebar:not(.collapsed) .nav-icon {
                margin-right: 15px;
            }
        }
    </style>
</head>
<body>
    <!-- 侧边栏 -->
    <div class="sidebar" id="sidebar">
        <div class="sidebar-header">
            <div class="logo">L</div>
            <div class="logo-text">Logo</div>
        </div>
        
        <button class="toggle-btn" id="toggleBtn">
            <span>≡</span>
        </button>
        
        <div class="nav-menu">
            <div class="nav-item active" data-tooltip="首页">
                <div class="nav-icon">🏠</div>
                <div class="nav-text">首页</div>
            </div>
            <div class="nav-item" data-tooltip="仪表板">
                <div class="nav-icon">📊</div>
                <div class="nav-text">仪表板</div>
            </div>
            <div class="nav-item" data-tooltip="项目">
                <div class="nav-icon">📁</div>
                <div class="nav-text">项目</div>
            </div>
            <div class="nav-item" data-tooltip="团队">
                <div class="nav-icon">👥</div>
                <div class="nav-text">团队</div>
            </div>
            <div class="nav-item" data-tooltip="设置">
                <div class="nav-icon">⚙️</div>
                <div class="nav-text">设置</div>
            </div>
            <div class="nav-item" data-tooltip="帮助">
                <div class="nav-icon">❓</div>
                <div class="nav-text">帮助</div>
            </div>
        </div>
    </div>
    
    <!-- 主内容区域 -->
    <div class="main-content">
        <div class="content-header">
            <h1>精美侧边栏演示</h1>
            <p>这是一个具有折叠功能和Tooltip提示的侧边栏示例</p>
        </div>
        
        <div class="card-container">
            <div class="card">
                <h3>折叠功能</h3>
                <p>点击侧边栏顶部的按钮可以折叠/展开侧边栏。折叠后，菜单项会显示为图标，鼠标悬浮时会出现Tooltip提示。</p>
            </div>
            <div class="card">
                <h3>流畅动画</h3>
                <p>使用CSS3的transition和cubic-bezier函数实现流畅自然的过渡动画效果，提升用户体验。</p>
            </div>
            <div class="card">
                <h3>响应式设计</h3>
                <p>在小屏幕设备上，侧边栏默认折叠以节省空间，确保在各种设备上都有良好的显示效果。</p>
            </div>
        </div>
    </div>

    <script>
        // 获取DOM元素
        const sidebar = document.getElementById('sidebar');
        const toggleBtn = document.getElementById('toggleBtn');
        let tooltipElement = null;
        
        // 创建全局Tooltip元素
        function createTooltip() {
            if (!tooltipElement) {
                tooltipElement = document.createElement('div');
                tooltipElement.className = 'tooltip';
                document.body.appendChild(tooltipElement);
            }
            return tooltipElement;
        }
        
        // 显示Tooltip
        function showTooltip(element, text) {
            const tooltip = createTooltip();
            tooltip.textContent = text;
            
            const rect = element.getBoundingClientRect();
            tooltip.style.top = rect.top + (rect.height / 2) - (tooltip.offsetHeight / 2) + 'px';
            tooltip.style.left = '70px';
            
            // 触发动画
            setTimeout(() => {
                tooltip.style.opacity = '1';
                tooltip.style.visibility = 'visible';
                tooltip.style.transform = 'translateX(0)';
            }, 10);
        }
        
        // 隐藏Tooltip
        function hideTooltip() {
            if (tooltipElement) {
                tooltipElement.style.opacity = '0';
                tooltipElement.style.visibility = 'hidden';
                tooltipElement.style.transform = 'translateX(-10px)';
            }
        }
        
        // 切换侧边栏状态
        toggleBtn.addEventListener('click', function() {
            sidebar.classList.toggle('collapsed');
            
            // 更新按钮图标
            const icon = this.querySelector('span');
            if (sidebar.classList.contains('collapsed')) {
                icon.textContent = '≡';
            } else {
                icon.textContent = '×';
                // 展开时隐藏Tooltip
                hideTooltip();
            }
        });
        
        // 菜单项鼠标事件
        const navItems = document.querySelectorAll('.nav-item');
        navItems.forEach(item => {
            item.addEventListener('mouseenter', function(e) {
                if (sidebar.classList.contains('collapsed')) {
                    const tooltipText = this.getAttribute('data-tooltip');
                    showTooltip(this, tooltipText);
                }
            });
            
            item.addEventListener('mouseleave', function(e) {
                if (sidebar.classList.contains('collapsed')) {
                    hideTooltip();
                }
            });
        });
        
        // 菜单项点击事件
        navItems.forEach(item => {
            item.addEventListener('click', function() {
                // 移除所有项的active类
                navItems.forEach(navItem => navItem.classList.remove('active'));
                // 为当前点击项添加active类
                this.classList.add('active');
                // 点击时隐藏Tooltip
                hideTooltip();
            });
        });
        
        // 页面点击时隐藏Tooltip
        document.addEventListener('click', function() {
            if (sidebar.classList.contains('collapsed')) {
                hideTooltip();
            }
        });
    </script>
</body>
</html>
